package com.sccba.emtbusiness.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sccba.common.utils.SecurityUtils;
import com.sccba.emtbusiness.domain.YjczEmergencyProcessTemplate;
import com.sccba.emtbusiness.service.IYjczEmergencyProcessTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sccba.emtbusiness.mapper.YjczEmergencySceneMapper;
import com.sccba.emtbusiness.domain.YjczEmergencyScene;
import com.sccba.emtbusiness.service.IYjczEmergencySceneService;

/**
 * 应急处置场景Service业务层处理
 * 
 * @author lichh
 * @date 2024-04-18
 */
@Service
public class YjczEmergencySceneServiceImpl implements IYjczEmergencySceneService 
{
    @Autowired
    private YjczEmergencySceneMapper yjczEmergencySceneMapper;

    @Autowired
    private IYjczEmergencyProcessTemplateService templateService;

    /**
     * 查询应急处置场景
     * 
     * @param pkId 应急处置场景主键
     * @return 应急处置场景
     */
    @Override
    public YjczEmergencyScene selectYjczEmergencySceneByPkId(Long pkId)
    {
        return yjczEmergencySceneMapper.selectYjczEmergencySceneByPkId(pkId);
    }

    /**
     * 查询应急处置场景列表
     * 
     * @param yjczEmergencyScene 应急处置场景
     * @return 应急处置场景
     */
    @Override
    public List<YjczEmergencyScene> selectYjczEmergencySceneList(YjczEmergencyScene yjczEmergencyScene)
    {
        return yjczEmergencySceneMapper.selectYjczEmergencySceneList(yjczEmergencyScene);
    }

    /**
     * 新增应急处置场景
     * 
     * @param yjczEmergencyScene 应急处置场景
     * @return 结果
     */
    @Override
    public int insertYjczEmergencyScene(YjczEmergencyScene yjczEmergencyScene)
    {
        return yjczEmergencySceneMapper.insertYjczEmergencyScene(yjczEmergencyScene);
    }

    /**
     * 修改应急处置场景
     * 
     * @param yjczEmergencyScene 应急处置场景
     * @return 结果
     */
    @Override
    public int updateYjczEmergencyScene(YjczEmergencyScene yjczEmergencyScene)
    {
        return yjczEmergencySceneMapper.updateYjczEmergencyScene(yjczEmergencyScene);
    }

    /**
     * 批量删除应急处置场景
     * 
     * @param pkIds 需要删除的应急处置场景主键
     * @return 结果
     */
    @Override
    public int deleteYjczEmergencySceneByPkIds(Long[] pkIds)
    {
        return yjczEmergencySceneMapper.deleteYjczEmergencySceneByPkIds(pkIds);
    }

    /**
     * 删除应急处置场景信息
     * 
     * @param pkId 应急处置场景主键
     * @return 结果
     */
    @Override
    public int deleteYjczEmergencySceneByPkId(Long pkId)
    {
        return yjczEmergencySceneMapper.deleteYjczEmergencySceneByPkId(pkId);
    }

    public List<YjczEmergencyScene> selectSceneNotSelected (String fbId, String channelId) {
        return yjczEmergencySceneMapper.selectSceneListNotSelected(fbId, channelId);
    }

    @Override
    public List<YjczEmergencyScene> selectSceneByFbAndChannel(String fbId, String channelId) {
        return yjczEmergencySceneMapper.selectSceneByFbAndChannel(fbId, channelId);
    }

    @Override
    public List<YjczEmergencyScene> selectAllScene() {
        return yjczEmergencySceneMapper.selectAllScene();
    }

    @Override
    public void addOrUpdateSceneAndProcess(YjczEmergencyScene yjczEmergencyScene) {
        Long pkId = yjczEmergencyScene.getPkId();
        List<YjczEmergencyProcessTemplate> templateList = yjczEmergencyScene.getTemplateList();
        if (null == pkId || 0 == pkId) {
            yjczEmergencySceneMapper.insertYjczEmergencyScene(yjczEmergencyScene);
            pkId = yjczEmergencyScene.getPkId();
            for (YjczEmergencyProcessTemplate template : templateList) {
                template.setNodeSceneId(pkId);
                template.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                template.setCreateTime(new Date());
            }
            templateService.batchInsert(templateList);
        } else {
            yjczEmergencySceneMapper.updateYjczEmergencyScene(yjczEmergencyScene);
            List<Long> needDeletePkId = new ArrayList<>();
            List<YjczEmergencyProcessTemplate> needAddList = new ArrayList<>();
            int length = templateList.size();
            YjczEmergencyProcessTemplate template;
            for (int i = length - 1; i >= 0; -- i) {
                template = templateList.get(i);
                Long tPkId = template.getPkId();
                if (null == tPkId || 0 == tPkId) {
                    needAddList.add(template);
                    templateList.remove(i);
                    template.setNodeSceneId(pkId);
                    template.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                    template.setCreateTime(new Date());
                    continue;
                }
                if ("2".equals(template.getDelFlag())) {
                    needDeletePkId.add(tPkId);
                    templateList.remove(i);
                }
                template.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
                template.setUpdateTime(new Date());
            }
            if (!needDeletePkId.isEmpty()) {
                templateService.deleteByPkIds(needDeletePkId);
            }
            if (!needAddList.isEmpty()) {
                templateService.batchInsert(needAddList);
            }
            if (!templateList.isEmpty()) {
                templateService.batchUpdate(templateList);
            }
        }
    }

    @Override
    public boolean checkSceneNameIsExists(YjczEmergencyScene scene) {
        return yjczEmergencySceneMapper.selectCountBySceneName(scene) > 0;
    }
}
